<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2021-04-28
 * Time: 17:10
 */

namespace app\services;


use app\models\UserAddress;
use app\models\Users;
use think\facade\Db;

class UserAddressService
{
    /**
     * @param $select
     * @param int $page
     * @param int $limit
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList($select, $page = 1, $limit = 10)
    {
        $address = UserAddress::lists($select, $page, $limit);

        return $address;
    }

    /**
     * @param $select
     * @return UserAddress|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAll($select)
    {
        $addresses = UserAddress::where($select['where']);

        if (isset($select['order'])) {
            $addresses = $addresses->order($select['order']);
        }

        $addresses = $addresses->select();

        return $addresses;
    }

    /**
     * @param $addressId
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDetail($addressId)
    {
        $address = UserAddress::where("addressId",$addressId)->find();

        if ($address instanceof UserAddress) {
            return $address;
        }

        throw new \Exception('地址不存在' , 500);
    }

    /**
     * @param $params
     * @return bool
     * @throws \Exception
     */
    public function create($params)
    {
        try {
            Db::startTrans();
            $address = new UserAddress();
            foreach ($params as $key => $val) {
                $address->$key = $val;
            }
            $address->save();
            if(isset($params["isDefault"]) && $params['isDefault']==1){
                UserAddress::where("userId",$address->userId)->where("addressId","<>",$address->addressId)->update(['isDefault'=>0]);
            }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            throw new \Exception($e->getMessage(), 500);
        }
    }


    /**
     * @param $params
     * @return bool
     * @throws \Exception
     */
    public function update(UserAddress $address, $params)
    {
        try {
            Db::startTrans();
            if(isset($params["isDefault"]) && $params['isDefault']==1){
                UserAddress::where("userId",$address->userId)->update(['isDefault'=>0]);
            }
            foreach ($params as $key => $val) {
                $address->$key = $val;
            }
            $address->save();
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            throw new \Exception($e->getMessage(), 500);
        }
    }

    /**
     * @param $params
     * @return bool
     * @throws \Exception
     */
    public function delete(UserAddress $address)
    {
        try {
            $address->delete();

            $count = UserAddress::where(['userId' => $address->userId])->count();
            if ($count == 1) {
                UserAddress::where(['userId' => $address->userId])->save(['isDefault' => 1]);
            }
            return true;



        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), 500);
        }
    }

    /**
     * @param $params
     * @return bool
     * @throws \Exception
     */
    public function setDefault(UserAddress $address, $isDefault = 0)
    {
        try {
            if ($isDefault == 1) {
                UserAddress::where(['userId' => $address->userId])->save(['isDefault' => 0]);
            }
            $address->isDefault = $isDefault;
            $address->save();
            return true;

        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), 500);
        }
    }

    /**
     * @param $params
     * @return bool
     * @throws \Exception
     */
    public function getDefaultAddress(Users $user)
    {
        try {
            $address = UserAddress::where(['userId' => $user->userId, 'isDefault' => 1])->find();

            return $address;

        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), 500);
        }
    }

  
}